function frmBegin2() {
    win2.onshow = function() {
        win2.say('Nuevo');
    }
}

var mngProfiles = new MngProfiles();

function newProfile() {
    win2.clearForm();
}

function showModule(modID, obj) {
	var mod = obj.parentNode;

	var dvPrg = get('divPrg' + modID);
	var chks = gtn(dvPrg, 'input');
	var nc = chks.length, i;

	if (!obj.checked) {
		dvPrg.className = 'none';
		mod.className = 'modulePlus';
	}	else {
		dvPrg.removeAttribute('class');
		mod.className = 'moduleLess';
	}
    
    for (i = 0; i < nc; i ++) chks[i].checked = obj.checked;
}

function setProgram(modID) {
	var dvPrg = get('divPrg' + modID);

	var chks = gtn('divPrg' + modID, 'input');
	var nc = chks.length;

	var ch = 0;
	for (var c = 0; c < nc; c ++) {
		if (chks[c].checked) ch ++;
	}

	var chkPrg = get('mod' + modID);
	if (ch == 0) {
		chkPrg.checked = false;
		dvPrg.className = 'none';
		get('mod' + modID).parentNode.className = 'modulePlus';
	}	else if (ch == nc) chkPrg.checked = true;
}

function saveProfile() {
	var profile = get('txtProfile').value;

	var prgs = '';
	var chks = gtn('fdsPermisos', 'input');
	var nc = chks.length;

	if (!profile || !nc) win2.say('Ambos campos son obligatorios', 'error');
	else {
		if (profile.length < 5) {
			win2.say('El perfil debe tener al menos 5 carácteres', 'error');
			return false;
		}

		for (var c = 0; c < nc; c ++) {
			var chk = chks[c];

			if (chk.value != 'on' && chk.checked) prgs += chk.value + ',';
		}
		prgs = prgs.substring(0, prgs.length - 1);

		if (!prgs) {
			win2.say('Debe seleccionar al menos un programa', 'error');
			return false;
		}
        prfID = toInt(get('hidProfileID').value);

		win2.loaProgram('Guardando ...');

		gMethods.groupMethods();
            mngProfiles.saveProfile(prfID, profile, prgs);
            mngProfiles.getTable();
        gMethods.execMethods();
		gMethods.onresponse = function() {
			if (this.response.saveProfile) {
                fillTable('tblProfiles', this.response.getTable);

                win2.stopLoading('Se ha guardado correctamente el Perfil');
                win2.clearForm();
            }   else win2.stopLoading('No se ha conseguido guardar el perfil');
		};
	}
    return true;
}

function editProfile(prfID) {
	win2.loaProgram('Procesando ...');

	mngProfiles.getProfile(prfID);
	mngProfiles.onresponse = function() {
		var profile = this.response;

		win2.clearForm();
		get('hidProfileID').value = prfID;
		get('txtProfile').value = profile.profile;

		var prgs = profile.programs;
		var np = prgs.length;
		//alert(prgs);
		//alert(np);

		for (var p = 0; p < np; p ++) {
			var chkPrg = get('prg' + prgs[p].id);
			chkPrg.checked = true;

			var divMod = chkPrg.parentNode.parentNode.parentNode;
			var chkMod = gtn(divMod, 'input')[0];
			chkMod.checked = true;
			var idMod = chkMod.id.substr(3);

			get('divPrg' + idMod).removeAttribute('class');
			gtn(divMod, 'label')[0].className = 'moduleLess';
		}

		win2.stopLoading('Modificando');
	};
}

function delProfile(lnk, prfID) {
	cConfirm.onOK = function() {

		win2.loaProgram('Eliminando ...');

		mngProfiles.deleteProfile(prfID);
		mngProfiles.onresponse = function() {
			if (this.response) {
				removeElement(lnk.parentNode.parentNode);
				fillTable('tblProfiles');

				win2.stopLoading('Se ha eliminado correctamente el Perfil');
			}   else win2.stopLoading('No se ha conseguido eliminar el Perfil', 'error');
		};
	};
	cConfirm.show('Confirma que desea eliminar este perfil?');
}